package com.itheima.web.serlvlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceimpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {
    BrandService brandService = new BrandServiceimpl();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Brand> brands = brandService.selectAll();
        String jsonString = JSON.toJSONString(brands);
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(jsonString);
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Brand brand1 = JSON.parseObject(s, Brand.class);
//        Brand brand = JSON.parseObject(s, Brand.class);
        brandService.add(brand1);
//        System.out.println(brand1);
        response.getWriter().write("success");
    }
    public void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收数据[1,2,3]
        String updateBrandId = request.getParameter("updateBrandId");
//        把json转成数组
        int i = Integer.parseInt(updateBrandId);
        //int[] ids = JSON.parseObject(s, int[].class);
        List<Brand> brand = brandService.selectById(i);
        String jsonString = JSON.toJSONString(brand);
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(jsonString);


    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);
        brandService.update(brand);
        response.getWriter().write("success");
    }
    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收数据[1,2,3]
        BufferedReader reader = request.getReader();
        String s = reader.readLine();

//        把json转成数组
        int i = JSONArray.parseObject(s, int.class);
        //int[] ids = JSON.parseObject(s, int[].class);
        brandService.deleteById(i);
        response.getWriter().write("success");
    }
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收数据[1,2,3]
        BufferedReader reader = request.getReader();
        String s = reader.readLine();

//        把json转成数组
        int[] ints = JSONArray.parseObject(s, int[].class);
        //int[] ids = JSON.parseObject(s, int[].class);
        brandService.deleteByIds(ints);
        System.out.println(ints);
        response.getWriter().write("success");
    }
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收前端数据并处理
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int  currentPage= Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        //进行方法调用
        PageBean<Brand> selectByPage = brandService.selectByPage(currentPage, pageSize);
        //将数据转换成JSON
        String jsonString = JSON.toJSONString(selectByPage);
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(jsonString);
    }
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收前端数据并处理get()请求
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int  currentPage= Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //接收数据post()请求
        BufferedReader reader = request.getReader();
        String string = reader.readLine();
        Brand brand = JSON.parseObject(string, Brand.class);

        //进行方法调用
        PageBean<Brand> selectByPage = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
        //将数据转换成JSON
        String jsonString = JSON.toJSONString(selectByPage);
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(jsonString);
    }
}
